<?php

class MD_Gallery extends ModelBase {

    function MD_Gallery(&$database) {
	$this->tableName = TABLE_GALLERY;
	$this->keyField = "id";
	ModelBase::ModelBase($database);
    }

    function getById($id) {
	$sql = "SELECT *  FROM $this->tableName ";
	if ($id != "") {
	    $sql .= " WHERE $this->keyField =$id ";
	}
	return $this->db->retrieve($sql);
    }

    function get_gallery_type_by_Where($where, $order, $limit) {
	$sql = "SELECT *  FROM tbl_gallery_type";
	if ($where != "") {
	    $sql .= " WHERE $where ";
	}
	if ($order != "") {
	    $sql .= " ORDER BY $order ";
	}
	if ($limit != "") {
	    $sql .= " LIMIT $limit ";
	}
//	echo $sql."<br/>";
	return $this->db->retrieve($sql);
    }

    function get_gallery_type_by_Where_1($where, $order, $limit) {
	$sql = "SELECT *  FROM tbl_gallery_type";
	if ($where != "") {
	    $sql .= " WHERE $where ";
	}
	if ($order != "") {
	    $sql .= " ORDER BY $order ";
	}
	if ($limit != "") {
	    $sql .= " LIMIT $limit ";
	}
//	echo $sql."<br/>";
	return $this->db->getFirstRecord($sql);
    }

    function delete_pic($id) {
	$sql = "DELETE FROM $this->tableName WHERE $this->keyField =$id";
//	echo $sql;
	return $this->db->query($sql);
    }

    function edit_pic(&$arrayData) {
	$sql = "UPDATE  " . $this->tableName . " SET ";
	$sql .= "name_pic='" . $this->replaceString(&$arrayData["name_pic"]) . "'";
	$sql .= ",notes_pic='" . $this->replaceString(&$arrayData["notes_pic"]) . "'";
	$sql .= ",type_id='" . $this->replaceString(&$arrayData["type_id"]) . "'";
	if ($_FILES["url_pic"]['name'] != "") {
	    $sql .= ",url_pic='" . $_SESSION['Pkd_images2'] . "'";
	}
	$sql .= " WHERE $this->keyField=" . $arrayData["id"];
	$this->db->query($sql);
    }

    function edit_pic_slide(&$arrayData) {
	$sql = "UPDATE  " . $this->tableName . " SET ";
	$sql .= "name_pic='" . $this->replaceString(&$arrayData["name_pic"]) . "'";
	if ($_FILES["url_pic"]['name'] != "") {
	    $sql .= ",url_pic='" . $_SESSION['Pkd_images2'] . "'";
	}
	$sql .= " WHERE $this->keyField=" . $arrayData["id"];
	$this->db->query($sql);
    }

    function add_pic(&$arrayData) {
	$listFilds .= "name_pic";
	$listValues .= "'" . $this->replaceString(&$arrayData["name_pic"]) . "'";
	$listFilds .= ",notes_pic";
	$listValues .= ",'" . $this->replaceString(&$arrayData["notes_pic"]) . "'";
	$listFilds .= ",type_id";
	$listValues .= ",'" . $this->replaceString(&$arrayData["type_id"]) . "'";
	
//		$list_type = "";
//	$arr_type_id = $arrayData["type_id"];
//	if ($arr_type_id != "") {
//	    foreach ($arr_type_id as $value) {
//		$list_type .= "," . $value;
//	    }
//	    $list_type .= ",";
//	}
//	$listFilds .= ",type_id";
//	$listValues .= ",'" . $list_type . "'";
	
	if ($_FILES["url_pic"]['name'] != "") {
	    $listFilds .= ",url_pic";
	    $listValues .= ",'" . $_SESSION['Pkd_images2'] . "'";
	}
	$sql = "INSERT INTO $this->tableName (  $listFilds ) ";
	$sql .= " VALUES ($listValues ); ";
//	echo $sql."<br/>";
//	die();
	$this->db->query($sql);
    }

    function update_type(&$arrayData) {
	$sql = "UPDATE  tbl_gallery_type SET ";
	$sql .= "gallery_type_name='" . $this->replaceString(&$arrayData["gallery_type_name"]) . "'";
	$sql .= ",gallery_type_note='" . $this->replaceString(&$arrayData["gallery_type_note"]) . "'";
	$sql .= ",parent_id='" . $this->replaceString(&$arrayData["parent_id"]) . "'";

	$sql .= " WHERE $this->keyField=" . $arrayData["type_id"];
//	echo $sql;
//	die();
	$this->db->query($sql);
    }

    function delete_type($id) {
	$sql = "DELETE FROM tbl_gallery_type WHERE $this->keyField =$id";
//	echo $sql;
	return $this->db->query($sql);
    }

    function insert_type(&$arrayData) {
	$listFilds .= "gallery_type_name";
	$listValues .= "'" . $this->replaceString(&$arrayData["gallery_type_name"]) . "'";
	$listFilds .= ",gallery_type_note";
	$listValues .= ",'" . $this->replaceString(&$arrayData["gallery_type_note"]) . "'";
	$listFilds .= ",parent_id";
	$listValues .= ",'" . $this->replaceString(&$arrayData["parent_id"]) . "'";

	$sql = "INSERT INTO tbl_gallery_type (  $listFilds ) ";
	$sql .= " VALUES ($listValues ); ";
//	echo $sql;
//	die();
	$this->db->query($sql);
    }

    function upload_slide_pic($type) {
	$array_t = $_SESSION['ARRAY_IMG'];
	foreach ($array_t as $value) {
	    $listFilds = "";
	    $listValues = "";

	    $listFilds .= "url_pic";
	    $listValues .= "'" . $value . "'";
	    if ($type != "") {
		$listFilds .= ",type_id";
		$listValues .= ",'" . $type . "'";
	    }
	    $sql = "INSERT INTO $this->tableName ($listFilds) ";
	    $sql .= " VALUES ($listValues); ";
//	echo $sql."<br/>";
	    $this->db->query($sql);
	}
    }

}

?>